//
//  MineViewController.m
//  Business Club
//
//  Created by 范琦 on 16/4/7.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "MineViewController.h"
#import "MineTableViewCell.h"
#import "MineHeaderTableViewCell.h"
#import "MineDetailViewController.h"
#import "MineCollectViewController.h"
#import "MineEventViewController.h"
#import "MineServiceViewController.h"
#import "MineHeaderModel.h"
#import "UIViewController+HUD.h"
//#import "LogInViewController.h"
#import "BaseNavigationController.h"

static NSString *const reuseMineTableViewCell = @"MineTableViewCell";
static NSString *const reuseMineHeaderTableViewCell = @"MineHeaderTableViewCell";

@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSString *token;
}
@property (nonatomic, strong) UITableView *mineTableView;
@property (nonatomic, strong) NSMutableArray *listArr;


@end

@implementation MineViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的";
    self.view.backgroundColor = [UIColor huiseColor];

    token = [NSUSERDEFAULTS objectForKey:@"gettoken"];
    NSLog(@"----%@",token);
    [self creatView];
//    [self getData];

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    _mineTableView.frame = CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT);
    [_mineTableView reloadData];
}

- (void)creatView{
    
    self.mineTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) style:UITableViewStyleGrouped];
    [self.view addSubview:self.mineTableView];
    self.mineTableView.delegate = self;
    self.mineTableView.dataSource = self;
//    self.mineTableView.rowHeight = KSCREENHEIGHT / 7;
    _mineTableView.sectionFooterHeight = 0;
    [_mineTableView registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:nil] forCellReuseIdentifier:reuseMineTableViewCell];
    [_mineTableView registerNib:[UINib nibWithNibName:@"MineHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:reuseMineHeaderTableViewCell];
}

- (void)getData
{
    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:[NSString stringWithFormat:@"/My/myInfo.html?token=%@", token] RegisterApiMethod:RequestMethodGet];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        self.listArr = [NSMutableArray array];
        NSDictionary *dic = [request.responseBody objectForKey:@"result"];
//        if ([request.responseBody[@"error"] isEqualToString:@"0"]) {
//            NSDictionary *dic = [result objectForKey:@"result"];
        
                MineHeaderModel *model = [[MineHeaderModel alloc]init];
                model.nickname = [dic objectForKey:@"nickname"];
                model.company_name = [dic objectForKey:@"company_name"];
                model.avatar = [dic objectForKey:@"avatar"];
                model.sex = [dic objectForKey:@"sex"];
                [self.listArr addObject:model];
            
            [_mineTableView reloadData];
//        }
        
    } failure:^(YTKBaseRequest *request) {

    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor huiseColor];
    return view;
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 140 * KSCREENHEIGHT / 667;
    } else {
        return 44;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MineDetailViewController *detailVC = [[MineDetailViewController alloc] init];
        [self.navigationController pushViewController:detailVC animated:YES];
    } else if (indexPath.section == 1){
        MineServiceViewController *serviceVC = [[MineServiceViewController alloc] init];
        [self.navigationController pushViewController:serviceVC animated:YES];
    } else if (indexPath.section == 2) {
        MineEventViewController *eventVC = [[MineEventViewController alloc] init];
        [self.navigationController pushViewController:eventVC animated:YES];
    } else if (indexPath.section == 3){
        MineCollectViewController *collectVC = [[MineCollectViewController alloc] init];
        [self.navigationController pushViewController:collectVC animated:YES];
    } else if (indexPath.section == 4){
//        [self showHint:@"敬请期待" yOffset:-300];
//        CircleOtherViewController *circle = [[CircleOtherViewController alloc] init];
//        circle.state = @"my";
//        [self.navigationController pushViewController:circle animated:YES];
        
        
    } else{
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"" message:@"确定要退出吗？" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", @"取消", nil];
        [alter show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
        
        [NSUSERDEFAULTS setBool:NO forKey:@"is_logIn"];
        [NSUSERDEFAULTS synchronize];
        
        
//        LogInViewController *LogInVC = [[LogInViewController alloc] init];
//        BaseNavigationController * navigation = [[BaseNavigationController alloc]initWithRootViewController:LogInVC];
        
//        [self presentViewController:navigation animated:YES completion:nil];
    
//        [self presentViewController:LogInVC animated:YES completion:^{
//        
//        }];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineTableViewCell *mine = [tableView dequeueReusableCellWithIdentifier:reuseMineTableViewCell];
    [mine setSelectionStyle:UITableViewCellSelectionStyleNone];// 去掉点击之后的阴影
    MineHeaderTableViewCell *headerCell = [tableView dequeueReusableCellWithIdentifier:reuseMineHeaderTableViewCell];
    [headerCell setSelectionStyle:UITableViewCellSelectionStyleNone];// 去掉点击之后的阴影
    if (indexPath.section == 0) {
        MineHeaderModel *model = self.listArr[0];
        headerCell.nameLabel.text = model.nickname;
        headerCell.companyName.text = model.company_name;
        [headerCell.titleImage sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:nil];
//        headerCell.titleImage.layer.cornerRadius = 32.5;
//        headerCell.layer.masksToBounds = YES;
        headerCell.str = @"1";
        if ([model.sex isEqualToString:@"1"]) {
            headerCell.genderImage.image = [UIImage imageNamed:@"iconfont-nan"];
        } else if ([model.sex isEqualToString:@"2"]) {
            headerCell.genderImage.image = [UIImage imageNamed:@"iconfont-nv"];
        }
        return headerCell;
    }
    if (indexPath.section == 1) {
        mine.titleLabel.text = @"我的库存";
        mine.iconImage.image = [UIImage imageNamed:@"iconfont-kucun"];
    } else if (indexPath.section == 2) {
        mine.titleLabel.text = @"我的商品";
        mine.iconImage.image = [UIImage imageNamed:@"iconfont-dingdan"];
    } else if (indexPath.section == 3){
        mine.titleLabel.text = @"我的订单";
        mine.iconImage.image = [UIImage imageNamed:@"iconfont-dingdan"];

    } else  if (indexPath.section == 4){
        mine.titleLabel.text = @"关于我们";
        mine.iconImage.image = [UIImage imageNamed:@"iconfont-women"];
    }
    return mine;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    [self getData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
